﻿/**
  *FileName:    mandelbrot
  *Date:        23/4/11 周二 下午 3:25:18
  *Author:      Zhou Hang
  *Version:     1.0
  *Description: code adapted from https://www.geeksforgeeks.org/fractals-in-cc/
*/

// C++ implementation for mandelbrot set fractals
#include <graphics.h>
#include <iostream>
#include <conio.h>
#define MAXCOUNT 30
#define M_WIDTH 600
#define M_HEIGHT 400

using namespace std;

// Function to draw mandelbrot set
void fractal(float left, float top, float xside, float yside)
{
    float xscale, yscale, zx, zy, cx, tempx, cy;
    int x, y;
    int maxx, maxy, count;

    maxx = M_WIDTH;
    maxy = M_HEIGHT;
    xscale = xside / maxx;
    yscale = yside / maxy;

    // scanning every point in that rectangular area.
    // Each point represents a Complex number (x + yi).
    // Iterate that complex number
    for (y = 1; y <= maxy - 1; y++) {
        for (x = 1; x <= maxx - 1; x++)
        {
            // c_real
            cx = x * xscale + left;
            // c_imaginary
            cy = y * yscale + top;
            // z_real
            zx = 0;
            // z_imaginary
            zy = 0;
            count = 0;

            // Calculate whether c(c_real + c_imaginary) belongs
            // to the Mandelbrot set or not and draw a pixel
            // at coordinates (x, y) accordingly
            // If you reach the Maximum number of iterations
            // and If the distance from the origin is
            // greater than 2 exit the loop
            while ((zx * zx + zy * zy < 4) && (count < MAXCOUNT))
            {
                // Calculate Mandelbrot function
                // z = z*z + c where z is a complex number

                // tempx = z_real*_real - z_imaginary*z_imaginary + c_real
                tempx = zx * zx - zy * zy + cx;

                // 2*z_real*z_imaginary + c_imaginary
                zy = 2 * zx * zy + cy;

                // Updating z_real = tempx
                zx = tempx;

                // Increment count
                count++;
            }
            
            // To display the created fractal
            putpixel(x, y, RGB(0, count * 100, count * 90));
        }
    }
}

// Driver code
int mandelgret()
{
    float left, top, xside, yside;

    // setting the left, top, xside and yside
    // for the screen and image to be displayed
    left = -1.75;
    top = -0.25;
    xside = 0.25;
    yside = 0.45;
    char driver[] = "";

    // initgraph initializes the
    // graphics system by loading a
    // graphics driver from disk
    initgraph(M_WIDTH, M_HEIGHT);

    // Function calling
    fractal(left, top, xside, yside);

    int key = _getch();

    // closegraph function closes the
    // graphics mode and deallocates
    // all memory allocated by
    // graphics system
    closegraph();

    return 0;
}
